package com.yunhui.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;

import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;
/**
 * 短信工具类
 * @author Administrator
 *
 */
@Component
public class SmsUtil {

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)

    //accessKeyId的值
//    @Value("${aliyun.sms.accessKeyId}")
//    private  String accessKeyId;
//    //模板号
//    @Value("${aliyun.sms.template_code}")
//    private String template_code;
//    //签名名称
//    @Value("${aliyun.sms.sign_name}")
//    private  String sign_name;
//    //
//    @Value("${aliyun.sms.accessKeySecret}")
//    private String accessKeySecret;

    @SuppressWarnings("deprecation")
	public Integer sendSms(String mobile,Integer code) throws ClientException {
        String accessKeyId ="LTAI4FnfSUTDjpoyHXtwkBsU";
        String accessKeySecret = "OI7Ok6CNVhGpgJhjonSnZyF5NajMpM";
        String template_code = "SMS_177250745";
        String sign_name = "小脑唬";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", sign_name);
        request.putQueryParameter("TemplateCode",template_code);

        request.putQueryParameter("TemplateParam", "{code:"+code+"}");
        CommonResponse response  =null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(response.getHttpStatus());
        return response.getHttpStatus();
    }
}
